package com.zhubayi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhubayi.constant.MyConstant;
import com.zhubayi.entity.R;
import com.zhubayi.entity.Great;
import com.zhubayi.entity.User;
import com.zhubayi.entity.vo.UserVo;
import com.zhubayi.exception.AccountExistException;
import com.zhubayi.exception.AccountNotFundException;
import com.zhubayi.exception.AccountWrongPasswordException;
import com.zhubayi.mapper.UserMapper;
import com.zhubayi.service.FavorService;
import com.zhubayi.service.UserService;
import com.zhubayi.utils.FileUtils;
import com.zhubayi.utils.UserQueryConditionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 祝八一
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    FavorService favorService;
    @Override
    public void register(User user) {
        User user1 = userMapper.queryByName(user.getAccount());
        if (user1!=null){
            throw new AccountExistException(MyConstant.ACCOUNTEXIST.getMessage());
        }
        userMapper.add(user);
    }

    @Override
    public User login(String username, String password) {
        User user1 = userMapper.queryByName(username);
        if (user1==null){
            throw new AccountNotFundException(MyConstant.ACCOUNTNOTFOUNT.getMessage());
        }
        if (!user1.getPassword().equals(password)){
            throw new AccountWrongPasswordException(MyConstant.ACCOUNTWRONGPASSWORD.getMessage());
        }
        User user = new User();
        BeanUtils.copyProperties(user1,user);
        user.setPassword("");
        return user;
    }

    @Override
    public void updateAvatar(String url,Integer id) {
        userMapper.updateAvatar(url,id);
    }

    @Override
    public void addVideoNum(Integer id) {
        userMapper.addVideoNumAndScore(id);
    }

    @Override
    public PageInfo<UserVo>  queryCondition(UserQueryConditionUtils utils,Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize).setOrderBy("id desc");
        List<User> userList = userMapper.queryCondition(utils);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        System.out.println(pageInfo);
        List<UserVo> collect = pageInfo.getList().stream().map(i -> {
            UserVo userVo = new UserVo();
            Integer num = favorService.queryCountByUid(i.getId());
            BeanUtils.copyProperties(i, userVo);
            userVo.setFavorNum(num);
            return userVo;
        }).collect(Collectors.toList());
        PageInfo<UserVo> userVoPageInfo = new PageInfo<>();
        BeanUtils.copyProperties(pageInfo,userVoPageInfo);
        userVoPageInfo.setList(collect);
        return userVoPageInfo;
}



    @Override
    public PageInfo<UserVo> queryAll(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize).setOrderBy("id desc");
        List<User> userList = userMapper.queryAllUser();
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        System.out.println(pageInfo);
        List<UserVo> collect = pageInfo.getList().stream().map(i -> {
            UserVo userVo = new UserVo();
            Integer num = favorService.queryCountByUid(i.getId());
            BeanUtils.copyProperties(i, userVo);
            userVo.setFavorNum(num);
            return userVo;
        }).collect(Collectors.toList());
        PageInfo<UserVo> userVoPageInfo = new PageInfo<>();
        BeanUtils.copyProperties(pageInfo,userVoPageInfo);
        userVoPageInfo.setList(collect);
        return userVoPageInfo;
    }

    @Override
    public void download(HttpServletResponse response, List<UserVo> userVoList) {
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (userVoList.size()>0&&userVoList!=null){
            int i=1;
            for (UserVo userVo : userVoList) {
                LinkedHashMap<String, Object> map = new LinkedHashMap<>();
                map.put("序号",i);
                map.put("id号",userVo.getId());
                map.put("用户名",userVo.getAccount());
                map.put("邮箱",userVo.getEmail());
                map.put("注册时间",dateFormat.format(userVo.getRegTime()));
                map.put("发布视频数",userVo.getVideoNum());
                map.put("收藏视频数",userVo.getFavorNum());
                map.put("积分",userVo.getScore());
                if (userVo.getStatus()){
                    map.put("是否禁用","禁用");
                }else {
                    map.put("是否禁用","未禁用");
                }
                i++;
                list.add(map);
            }
        }

        try {
            FileUtils.downloadExcel(list,response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(User user) {

    }

    @Override
    public R updatePassword(Integer id, String oldpassword, String password) {
        User user = userMapper.queryById(id);
        if (!user.getPassword().equals(oldpassword)){
            return new R(MyConstant.ACCOUNTWRONGPASSWORD);
        }
        user.setPassword(password);
        userMapper.update(user);
        return new R(MyConstant.OK);
    }

    @Override
    public User findByUsername(String username) {
    return     userMapper.queryByName(username);
    }

    @Override
    public List<Great> SelectRoleByUid(Integer id) {
      return   userMapper.queryRolesByUId(id);

    }
}
